#pragma once

#include "MemList.h"

class CMemChunks;

enum BlockType
{
    SMALL_BLOCK,
    LARGE_BLOCK
};

/**
 * 双向链表元素结构体
 * pList : 指向元素所在链表的类实例的指针，在仅有元素数据的情况下，可通过pList获得类实例
 */
struct ListPtr
{
};

typedef CMemList<ListPtr, CSpinLock> CBlockList;
typedef CBlockList::Node BlockNode;

class CMemBlocks
{
public:
    CMemBlocks(BlockType bt, unsigned int nIndx, unsigned int nSize, CMemChunks *mc);
    ~CMemBlocks();

    bool Initialize();
    bool Destroy();

    BlockType GetBlockType() { return m_bt; }
    unsigned int GetIndex() { return m_nIndx; }
    unsigned int GetBlockSize() { return m_nBlockSize; }

    void *Alloc(unsigned int nSize);
    bool Free(void *pBlock);

private:
    void FreeListNode(BlockNode *pNode);

private:
    const BlockType m_bt;
    const unsigned int m_nIndx;
    const unsigned int m_nBlockSize;
    CMemChunks *const m_pChunks;
    CBlockList m_lstUsing;
    CBlockList m_lstFree;
};
